

#include <windows.h>
#include <d2d1.h>



class Renderer
{
    public:
        Renderer(HWND hWnd);
        ~Renderer();

        bool Initialize();
        void ClearColor( float red, float green, float blue, float alpha );

    private:
        HWND hWnd_;
        ID2D1Factory* pD2DFactory_;
        ID2D1HwndRenderTarget* pRenderTarget_;
};

Renderer::Renderer(HWND hWnd) : hWnd_(hWnd), pD2DFactory_(nullptr), pRenderTarget_(nullptr)
{
    if (!Initialize())
    {
        MessageBox(nullptr, TEXT("Renderer initialization failed!"), TEXT("Error"), MB_ICONERROR);
        return;
    }
}

Renderer::~Renderer()
{
    if (pRenderTarget_)
    {
        pRenderTarget_->Release();
    }
    if (pD2DFactory_)
    {
        pD2DFactory_->Release();
    }
}

bool Renderer::Initialize()
{
    // Create Direct2D factory
    HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory_);

    if (FAILED(hr))
    {
        return false;
    }

    // Create render target
    RECT rc;
    GetClientRect(hWnd_, &rc);
    D2D1_SIZE_U size = D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top);
    hr = pD2DFactory_->CreateHwndRenderTarget(D2D1::RenderTargetProperties(),
        D2D1::HwndRenderTargetProperties(hWnd_, size), &pRenderTarget_);

    

    if (FAILED(hr))
    {
        return false;
    }

    return true;
}

void Renderer::ClearColor( float red, float green, float blue, float alpha )
{
    pRenderTarget_->BeginDraw();
    pRenderTarget_->Clear( D2D1::ColorF( red, green, blue, alpha ) );
    pRenderTarget_->EndDraw();
}



LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

int main() {
    // Register window class
    WNDCLASSEX wcex;
    ZeroMemory(&wcex, sizeof(wcex));
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WindowProc;
    wcex.hInstance = GetModuleHandle(NULL);
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)::GetStockObject(BLACK_BRUSH);
    // black
    wcex.lpszClassName = TEXT("RenderWindowClass");

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(nullptr, TEXT("Window registration failed!"), TEXT("Error"), MB_ICONERROR);
        return -1;
    }

    // Create window
    RECT rc = { 0, 0, 800, 600 };
    AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
    HWND hWnd = CreateWindow(
        TEXT("RenderWindowClass"), 
        TEXT("AenoEngine"), 
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        rc.right - rc.left, 
        rc.bottom - rc.top,
        nullptr, 
        nullptr, 
        GetModuleHandle(NULL), 
        nullptr
        );

    if (!hWnd)
    {
        MessageBox(nullptr, TEXT("Window creation failed!"), TEXT("Error"), MB_ICONERROR);
        return -1;
    }

    // Initialize renderer
    Renderer renderer(hWnd);
    if (!renderer.Initialize())
    {
        return -1;
    }

    // Show window
    ShowWindow(hWnd, SW_SHOWDEFAULT);
    UpdateWindow(hWnd);

    // Main message loop
    MSG msg;
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);

        // Render a frame
        // renderer.Render();
        renderer.ClearColor(1.f,1.f,1.f,1.f);
    }

    return (int)msg.wParam;
}